<?php 
namespace app\modules\cn\models;
use yii\db\ActiveRecord;

class ExerciseCategory extends ActiveRecord {

    public static function tableName(){
        return '{{%exercise_category}}';
    }

    /**
     * @Title  [title]
     * @Author Don
     * @Date   2021-04-29
     * @param  [type]
     * @return [type]
     */
    public function getWriteExerciseByType($pid)
    {
        $res = self::find()->where(['pid'=>$pid])->asArray()->all();
        $cate_ids = array_column($res, 'cate_id');
        $cate_list = ExerciseCategoryList::find()->where(['in', 'cate_id', $cate_ids])->asArray()->all();
        // f_d($cate_list);
        $content_ids = array_column($cate_list, 'content_id');
        // f_d($content_ids);
        $data = ExerciseList::find()->where(['in', 'content_id', $content_ids])->asArray()->all();
        // f_d($data);
        return $data;
    }

    /**
     * @Title  [title]
     * @Author Don
     * @Date   2021-04-29
     * @param  [type]
     * @return [type]
     */
    public function getSpeakCateList($cate_id,$show=0)
    {
        // 查父级cate_id
        $res = self::find()->select('pid')->where(['cate_id'=>$cate_id])->asArray()->one();
        $pid = $res['pid'];
        // 查询父级下面所有的子集
        $where = "pid=$pid";
        if($show==1){
            $where .= " and isShow=1";
        }
        $data = self::find()->select('*,cate_id as catId')->where($where)->asArray()->all();

        return $data;
    }

    /**
     * [根据pid查询子cateid]
     * @Title  [title]
     * @Author Don
     * @Date   2021-05-11
     * @param  [type]     $pid [description]
     * @return [type]          [description]
     */
    public function getAllCates($pid)
    {
        $res = self::find()->where(['pid'=>$pid])->asArray()->all();
        return $res;
    }
}
